<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="../assets/login.png" alt="Logo" class="logo" />
        <h2>基于GIS的高速公路道路标志标牌管理系统设计</h2>
      </div>
      <div class="form-group">
        <el-input
          v-model="username"
          placeholder="用户名"
          prefix-icon="el-icon-user"
        >
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="form-group">
        <el-input
          v-model="password"
          type="password"
          placeholder="密码"
          prefix-icon="el-icon-lock"
          @keyup.enter="handleLogin"
        >
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="form-tips">
        <p>提示: admin/123456 或 visitor/123456</p>
      </div>
      <el-button type="primary" :loading="loading" @click="handleLogin" class="login-button">登录</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/modules/user'

const router = useRouter()
const userStore = useUserStore()

const username = ref('')
const password = ref('')
const loading = ref(false)

const handleLogin = async () => {
  if (!username.value || !password.value) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    const success = userStore.login(username.value, password.value)
    if (success) {
      ElMessage.success(`欢迎回来，${username.value}！`)
      // 登录成功后跳转到MapLayout页面
      router.push('/map')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('登录出错:', error)
    ElMessage.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1976d2, #64b5f6);
}

.login-box {
  width: 380px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.login-header h2 {
  color: #1976d2;
  font-size: 24px;
  margin: 0;
}

.form-group {
  margin-bottom: 20px;
}

.form-tips {
  text-align: center;
  margin-bottom: 15px;
  color: #909399;
  font-size: 12px;
}

.login-button {
  width: 100%;
  padding: 12px;
  font-size: 16px;
}
</style>
